package cn.com.dao;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.com.entity.PexaminationForm;

public class PexaminationFormDao {
	//体检单的dao文件
	private SessionFactory sessionFac;

	public SessionFactory getSessionFac() {
		return sessionFac;
	}

	public void setSessionFac(SessionFactory sessionFac) {
		this.sessionFac = sessionFac;
	}
	Session session;
	Transaction tr;
	//增加体检单
	public boolean addpexaminationform(PexaminationForm pexaminationform){
		boolean bool=false;
		session=sessionFac.openSession();
		tr=session.beginTransaction();
		try {
			//添加系统当前时间
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");   
			String date=sdf.format(new Date()); 
			pexaminationform.setPexaminationtime(date);
			session.save(pexaminationform);
			tr.commit();
			bool=true;
			session.close();
			return bool;
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return bool;
		}
	}
	//删除体检单
	public boolean deletePexaminationForm(PexaminationForm pexaminationform){
		boolean b=false;
		try {
			session=sessionFac.openSession();
			tr=session.beginTransaction();
			session.delete(pexaminationform);
			tr.commit();
			session.close();
			b=true;
			return b;
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return b;
		}
	}
	//修改体检单
	public boolean updatePexaminationForm(PexaminationForm pexaminationform){
		boolean b=false;
		try {
			session=sessionFac.openSession();
			tr=session.beginTransaction();
			session.update(pexaminationform);
			tr.commit();
			session.close();
			b=true;
			return b;
			
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return b;
		}
	}
	//查询全部
	public List<PexaminationForm> selectAllPexaminationForm(){
		session=sessionFac.openSession();
		String hql="form PexaminationForm";
		Query query=session.createQuery(hql);
		List<PexaminationForm> list=query.list();
		if(list!=null){
			System.out.println("ooooooooooooooooooooo");
		}
		return list;
	}
	//通过用户id查询体检单
	public List<PexaminationForm> selectPexaminationFormByid(int consumerid){
		@SuppressWarnings("resource")
		ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		sessionFac=(SessionFactory)ctx.getBean("sessionFac");
		session=sessionFac.openSession();
		String hql="from PexaminationForm where consumerid=?";
		Query query=session.createQuery(hql);
		query.setInteger(0, consumerid);
		@SuppressWarnings("unchecked")
		List<PexaminationForm> list=query.list();
		return list;
	}
	
	/*public List<PexaminationForm> selectpexaminationform(){
		PexaminationForm pexaminationform=null;
		session=sessionFac.openSession();
		String hql="SELECT pexf.consumerid,pexf.pexaminationid"
				+"FROM PexaminationForm pexf,Consumer c,Pexamination pex"
				+"WHERE pexf.consumerid=c.consumerid"
				+"AND pexf.pexaminationid=pex.pexaminationid";
		Query query=session.createQuery(hql);
		List<PexaminationForm> list=query.list();
		if(list!=null){
			System.out.println("试一试："+list);
			pexaminationform=list.get(0);
		}
		return list;
	}*/
	
}
